package com.ljk.study6ormtkmybatispage.controller;

import com.github.pagehelper.PageInfo;
import com.ljk.study6ormtkmybatispage.entity.User;
import com.ljk.study6ormtkmybatispage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc:
 *
 * @author ljk
 * @date 2021/4/2 22:24
 */
@RestController
public class IndexController {

    @Autowired
    private UserService userService;

    /**
     * 根据用户名查询所有同名用户信息
     * @param name
     * @return
     */
    @RequestMapping("/user")
    public Map user(String name) {
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.findUserByName(name);
        map.put("data", users.toString());
        return map;
    }

    /**
     * 分页查询所有用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/userPage")
    public Map userPage(@RequestParam(required = true) Integer pageNum, @RequestParam(required = true) Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        PageInfo<User> userPage = userService.findAllUserPage(pageSize, pageNum);
        map.put("data", userPage.toString());
        return map;
    }

}
